package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.waypolice.manager.service.ActDefineService;
import com.waypolice.mapper.UpmsActDefineMapper;
import com.waypolice.mapper.handwriting.UpmsActDefineMapperSon;
import com.waypolice.pojo.UpmsActDefine;
import com.waypolice.pojo.UpmsActDefineExample;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author zyp
 * @description 工作流定义业务层实现类
 * @create 2019-02-26 13:44
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ActDefineServiceImpl implements ActDefineService {


    private static final Logger logger = LoggerFactory.getLogger(ActDefineServiceImpl.class);

    @Autowired
    private UpmsActDefineMapper upmsActDefineMapper;

    @Autowired
    private UpmsActDefineMapperSon upmsActDefineMapperSon;

    @Override
    public int insert(UpmsActDefine upmsActDefine) {
        int count = upmsActDefineMapper.insert(upmsActDefine);
        return count;
    }

    @Override
    public int updateByPrimaryKey(UpmsActDefine upmsActDefine) {
        int count = upmsActDefineMapper.updateByPrimaryKey(upmsActDefine);
        return count;
    }

    /**
     * @author:zyp
     * @description 获取工作流定义列表
     * @date: 2019/2/26 14:22
     * @return
     */
    @Override
    public Object actDefineList(int offset, int limit, String sort, String order, String search) {
        Map<String, Object> result = new HashMap<>(2);
        UpmsActDefineExample upmsActDefineExample=new UpmsActDefineExample();
        UpmsActDefineExample.Criteria criteria = upmsActDefineExample.createCriteria();
        //增加排序条件
        if(StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)){
            upmsActDefineExample.setOrderByClause(sort+" "+order);
        }
        if(StringUtils.isNotBlank(search)){
            criteria.andAdNameLike("%"+search+"%");
        }
        PageHelper.offsetPage(offset, limit, false);
        List<UpmsActDefine> rows =upmsActDefineMapper.selectByExample(upmsActDefineExample);
        int count = upmsActDefineMapper.countByExample(upmsActDefineExample);
        result.put("rows",rows);
        result.put("total",count);
        return result;
    }

    /**
     * @author:zyp
     * @description 根据流程关联修改当前使用状态
     * @date: 2019/2/26 14:21
     * @return 
     */
    @Override
    public int updateStatusByAdCode(String adCode) {
        int count = upmsActDefineMapperSon.updateStatusByAdCode(adCode);
        return count;
    }

    /**
     * @author:zyp
     * @description 根据id获取工作流定义的数据
     * @date: 2019/2/26 14:21
     * @return 
     */
    @Override
    public UpmsActDefine getActDefineById(int id) {
        UpmsActDefine upmsActDefine = upmsActDefineMapper.selectByPrimaryKey((long)id);
        return upmsActDefine;
    }

    /**
     * @author:zyp
     * @description 根据id删除工作流定义的数据
     * @date: 2019/2/26 14:22
     * @return 
     */
    @Override
    public int deleteDefineById(int id) {
        int count = upmsActDefineMapper.deleteByPrimaryKey((long)id);
        return count;
    }

    /**
     * @author:zyp
     * @description 根据code获取工作流的唯一标识
     * @date: 2019/3/12 15:07
     * @return 
     */
    @Override
    public UpmsActDefine getNewAct(String activitiType) {
        UpmsActDefineExample upmsActDefineExample = new UpmsActDefineExample();
        UpmsActDefineExample.Criteria criteria =  upmsActDefineExample.createCriteria();
        criteria.andAdCodeEqualTo(activitiType);
        criteria.andAdStatusEqualTo(1L);
        List<UpmsActDefine> upmsActDefines = upmsActDefineMapper.selectByExample(upmsActDefineExample);
        if(upmsActDefines==null||upmsActDefines.size()==0){
            return null;
        }
        return upmsActDefines.get(0);
    }

    /**
     * @author:zyp
     * @description 根据一些code 获取指定的工作流定义（目的拿到工作流的key）
     * @date: 2019/3/20 9:58
     * @return 
     */
    @Override
    public List<UpmsActDefine> getDefineByCodes(List<String> codes) {
        UpmsActDefineExample upmsActDefineExample = new UpmsActDefineExample();
        UpmsActDefineExample.Criteria criteria =  upmsActDefineExample.createCriteria();
        criteria.andAdCodeIn(codes);
//        criteria.andAdStatusEqualTo(1L);
        List<UpmsActDefine> upmsActDefines = upmsActDefineMapper.selectByExample(upmsActDefineExample);
        return upmsActDefines;
    }

}